#!/usr/bin/env python3

import os
import json
import requests
import img2pdf
from lxml import etree
import re

#解析request数据
print("输入Formdata\n")
line = input()
bookId=re.findall(r"bookId=(.*)&_time",line)
_timestamp=re.findall(r'_timestamp=(.*)&_nonce',line)
_nonce=re.findall(r'_nonce=(.*)&_sign',line)
_sign=re.findall(r'&_sign=(.*)',line)

class Spider(object):
    def getJson(self):
        # post发送的数据
        postData = {
        'bookId': bookId,
        '_timestamp': _timestamp,
        '_nonce': _nonce,
        '_sign': _sign
        }

        url = 'https://biz.codeup.cn/ebookservices/detail.do'

        response = requests.post(url,data=postData)
        r=response.json()

        with open("./detail.json","w",encoding='utf-8') as f:
            json.dump(r,f,ensure_ascii=False,indent=2)
            print("加载入文件完成...")

    def __init__(self, index, config='./config.json'):
        self.index = index
        with open(config, 'r') as f:
            self.config = json.loads(f.read())
        self.session = requests.Session()
        self.session.headers.update(self.config['spider']['headers'])

    @staticmethod
    def check_path(path):
        if not os.path.isdir(path):
            os.makedirs(path)
        return True

    def get_pagex(self, url):
        r = self.session.get(url)
        r.encoding = 'utf-8'
        return etree.HTML(r.text)

    def download(self, filename, url):
        r = self.session.get(url)
        with open(filename, 'wb') as f:
            f.write(r.content)
        return True

    def run(self):
        # get detail
        page_pool = []
        with open('./detail.json', 'r', encoding='utf-8') as f:
            detail = json.loads(f.read())
        detail = detail['data']['chapters']
        for chapter in detail:
            arr_imgResIdSigns = chapter['imgResIdSigns'].split(',')
            arr_imgResIds = chapter['imgResIds'].split(',')
            while arr_imgResIdSigns:
                page = (arr_imgResIdSigns.pop(0), arr_imgResIds.pop(0))
                page_pool.append(page)

        # download page
        self.check_path('./pages')
        url = 'https://mp.codeup.cn/resourceservice/mediaplay.do'
        for i, item in enumerate(page_pool):
            resIdSign, resId = item
            params = {'mediaType': 3, 'resId': resId, 'resIdSign': resIdSign}
            r = self.session.get(url, params=params)
            with open('./pages/%s.png' % str(i).zfill(3), 'wb') as f:
                f.write(r.content)
                print(i)

        #merge2pdf
        with open('./textbook.pdf', 'wb') as f:
            f.write(img2pdf.convert(['./pages/%s' % x for x in os.listdir('./pages/')]))


if __name__ == '__main__':
    spider = Spider(0)
    spider.getJson()
    spider.run()
